package com.taylor.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @Desc : NIO客户端
 * @Author ： taylor
 * @Date ：2021/6/26 22:22
 */
public class NIOClient {

    public static void main(String[] args) {

        //获取网络通道
        try (SocketChannel socketChannel = SocketChannel.open()) {
            //非阻塞
            socketChannel.configureBlocking(false);
            InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8888);

            if (!socketChannel.connect(socketAddress)){
                while (!socketChannel.finishConnect()){
                    System.out.println("尝试连接......");
                }
            }
            System.out.println("连接成功！准备发送数据......");
            String s = "hello NIO! netty！";
            ByteBuffer buffer = ByteBuffer.wrap(s.getBytes());
            socketChannel.write(buffer);
            System.in.read();


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
